package pannelli.sub;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

import oggetti.Cliente;
import oggetti.NonEditableDefaultTableModel;
import oggetti.Riparazione;
import pannelli.VisualizzaRiparazione;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class SubPannelloVisualizzaRiparazione extends javax.swing.JPanel {

    private JScrollPane jScrollPane1;
    private DefaultTableModel model;
    private boolean flag = false;
    private JTable tabellaRiparazioni;
    private VisualizzaRiparazione v;
    private int cliente;
    private static SubPannelloVisualizzaRiparazione instance;

    private SubPannelloVisualizzaRiparazione() {
        super();
    }

    public void init(int cliente) {
        this.cliente = cliente;
        initGUI();
        initTable();
    }

    public static SubPannelloVisualizzaRiparazione getInstance() {
        if (instance == null) {
            instance = new SubPannelloVisualizzaRiparazione();
        }
        return instance;
    }

    public void updateTable() {
        initTable();
    }

    public SubPannelloVisualizzaRiparazione(ArrayList<Riparazione> result) {
        super();
        initGUI();
        initTable(result);
    }

    public void initTable(ArrayList<Riparazione> c) {
        int a = model.getRowCount();
        for (int i = 0; i < a; i++) {
            model.removeRow(0);
        }

        for (Riparazione tmp : c) {
            model.addRow(tmp.toArray());
        }
    }

    public void updateTable(int cliente) {
        this.cliente = cliente;
        initTable();
    }

    public void initTable() {
        int a = model.getRowCount();
        for (int i = 0; i < a; i++) {
            model.removeRow(0);
        }
        ArrayList<Riparazione> c = new ArrayList<Riparazione>();
        if (cliente == -1) {
            c = new DB().getRiparzioni();
            if (c == null) {
                c = new ArrayList<Riparazione>();
            }
        } else {
            c = new DB().getRiparzioniFromCliente(cliente);
            if (c == null) {
                new ArrayList<Riparazione>();
            }
        }
        for (Riparazione tmp : c) {
            model.addRow(tmp.toArray());
        }
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    private void initGUI() {
        try {
            AnchorLayout thisLayout = new AnchorLayout();
            this.setLayout(thisLayout);
            this.setPreferredSize(new java.awt.Dimension(835, 300));
        } catch (Exception e) {
            e.printStackTrace();
        }
        model = new NonEditableDefaultTableModel();
        tabellaRiparazioni = new JTable(model);
        jScrollPane1 = new JScrollPane();
        this.add(jScrollPane1, new AnchorConstraint(1, 1000, 1001, 0, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
        jScrollPane1.setViewportView(tabellaRiparazioni);
        String[] columnNames = {"Scheda",
            "ID Cliente", "Preventivo", "IMEI", "Difetto", "Stato", "Utente", "Data Consegna", "Accettato", "Note Riparazione"};
        for (int i = 0; i < columnNames.length; i++) {
            model.addColumn(columnNames[i]);
        }

        tabellaRiparazioni.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent arg0) {
                if (arg0.getClickCount() >= 2) {
                   // if (!flag) {
                     //   flag = true;
                    if(v!=null){
                       v.setVisible(false);
                       v = null;
                       flag = false;
                    }
                    
                        int row = tabellaRiparazioni.getSelectedRow();
                        if (row >= 0) {
                            v = new VisualizzaRiparazione(getRiparazioneFromRow(row));
                            v.addWindowListener(new WindowAdapter() {

                                public void windowClosing(WindowEvent arg0) {
                                    flag = false;
                                }
                            });
                        }
                    //}
                }
            }
        });
        tabellaRiparazioni.getColumnModel().getColumn(0).setPreferredWidth(30);
    }

    public Riparazione getRiparazioneFromRow(int row) {
        int id = (Integer) tabellaRiparazioni.getValueAt(row, 0);
        Riparazione c = new DB().getRiparazioneFromScheda(id);
        return c;
    }
}
